home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE10 / PRINTING / LISTING3.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-22  |  2.2 KB  |  53 lines

  1. procedure TForm1.Print1Click(Sender: TObject);
  2. var
  3.   InfoSize: Integer; { used to determine size of memory to allocate for}
  4.   { a TBitmapInfo structure }
  5.   ImageSize: Integer; { Used to determine size of memory to allocatefor }
  6.   { bitmap bits }
  7.   Info: PBitmapInfo; { Pointer to a TBitmapInfo structure which }
  8.   { contains information on the dimensions and color }
  9.   { of a Windows device independent bitmap }
  10.   Image: Pointer; { Pointer to the DIB bits which is an array of bytes }
  11.   ImWidth, ImHeight: Integer; { Used for calculating size of image on the }
  12.   { destination canvas }
  13. begin
  14.   with Image1.Picture.Bitmap do begin
  15.     { Call GetDIBSizes which returns the amount of memory needed to
  16.       allocate for both the DIB info header and the DIB bitmap bits }
  17.     GetDIBSizes(Handle, InfoSize, ImageSize);
  18.     { Allocate memory for the info header based on the size obtained from
  19.       GetDIBSizes }
  20.     Info := MemAlloc(InfoSize);
  21.     try
  22.       { Allocate memory for the Image based on the size from GetDIBSizes }
  23.       Image := MemAlloc(ImageSize);
  24.       try
  25.         { Retrieve the color palette information, the info header and the
  26.           bitmap bits with the GetDIB procedure }
  27.         GetDIB(Handle, Palette, Info^, Image^);
  28.         with Info^.bmiHeader do begin
  29.           Printer.BeginDoc; // Start a print job
  30.           try
  31.             { Calculate the size of the output rectangle to which the
  32.               image will be drawn. This will be based on one-half of the
  33.               printer's page width }
  34.             ImWidth := Printer.PageWidth div 2;
  35.             ImHeight := trunc((ImWidth / biWidth) * biHeight);
  36.             { Draw the information from the source bitmap to the
  37.               destination device context, which is the printer's canvas }
  38.             StretchDIBits(Printer.Canvas.Handle, 0, 0, ImWidth,
  39.               ImHeight, 0, 0, biWidth, biHeight, Image, Info^,
  40.               DIB_RGB_COLORS, SRCCOPY);
  41.           finally
  42.             Printer.EndDoc; // End the print job
  43.           end;
  44.         end;
  45.       finally
  46.         FreeMem(Image, ImageSize); // Free the allocated memory
  47.       end;
  48.     finally
  49.       FreeMem(Info, InfoSize); // Free the allocated memory
  50.     end;
  51.   end;
  52. end;
  53.